package com.zhiqianbao.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.adapter.BeforeRepayListAdapter;
import com.zhiqianbao.entity.Loan;
import com.zhiqianbao.entity.Repay;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2016/2/25.
 */
public class BeforeRepayActivity extends BaseActivity {

    /**
     * 已还总额
     */
    TextView textViewDebtContent;
    /**
     * 未还总额
     */
    TextView textViewAccountMoneyContent;
    /**
     * 未还列表
     */
    ListView listViewBillingDateOfLoan;
    /**
     * 没有数据显示
     */
    RelativeLayout textviewDataNull;

    Loan loan;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1 :
                    Object object = msg.obj;
                    //没有还款数据
                    if (null == object) {
                        textviewDataNull.setVisibility(View.VISIBLE);
                        listViewBillingDateOfLoan.setVisibility(View.GONE);
                        textViewDebtContent.setText("0");
                        textViewAccountMoneyContent.setText("0");
                    } else {
                        textviewDataNull.setVisibility(View.GONE);
                        loan = (Loan) object;
                        String repayAmount = loan.getRepayAmount() == null ? "0" : loan.getRepayAmount().toString();
                        textViewDebtContent.setText(repayAmount);
                        String remainAmount = loan.getRemainAmount() == null ? "0" : loan.getRemainAmount().toString();
                        textViewAccountMoneyContent.setText(remainAmount);
                        List<Repay> repays = loan.getRepays();
                        //如果没有数据
                        if (repays.isEmpty()) {
                            textviewDataNull.setVisibility(View.VISIBLE);
                            listViewBillingDateOfLoan.setVisibility(View.GONE);
                        } else {
                            listViewBillingDateOfLoan.setAdapter(new BeforeRepayListAdapter(BeforeRepayActivity.this, repays));
                        }

                    }
                    break;
                case 0 :
                    showShortToast1("连接超时");
                    break;
            }
            closeWaitDialog();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_before_repay_single);
        init();

        textViewDebtContent = (TextView) findViewById(R.id.textView_debt_content);
        textViewAccountMoneyContent = (TextView) findViewById(R.id.textView_account_money_content);
        listViewBillingDateOfLoan = (ListView) findViewById(R.id.listView_billing_date_of_loan);
        textviewDataNull = (RelativeLayout) findViewById(R.id.textview_data_null);
        displayBeforeRepayList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        showShortToast1("走到ss");
    }

    private void displayBeforeRepayList() {
        listViewBillingDateOfLoan.setVisibility(View.VISIBLE);
        showWaitDialog();
        //请求应还款列表
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message message = new Message();
                Map<String, String> param = new HashMap<>();
                param.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                param.put("userId",Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                //应还款状态
                param.put("state", Constants.BEFORE_REPAYMENT);
                String result = HttpUtils.httpPostString(Constants.API_ROOT + "/repay/list", param, BeforeRepayActivity.this);
                if (StringUtils.isBlank(result)) {
                    message.what = 0;
                } else {
                    Result<Loan> data = Result.fromJson(result, Loan.class);
                    loan = data.getData();
                    message.what = 1;
                    message.obj = loan;
                }
                handler.sendMessage(message);
            }
        }).start();
    }
}
